﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Core.Views;
using Microsoft.Practices.Unity;

namespace Core.Presenters
{
    class BuildingPresenter
    {

        private IBuildingDAO _buildingDAO;
        private IDistrictDAO _districtDAO;
        private IBuildingView _buildingView;
        private IBuilding _building;

        public BuildingPresenter(IBuildingView buildingView, IBuilding building = null)
        {
            buildingView.SaveBuilding += SaveBuildingEvent;
            if (building == null)
            {
                _building = ResolverContainer.Resolver.Resolve<IBuilding>();
            }
            else
            {
                _building = building;
            }
            
            buildingView.Building.DataSource = _building;
            _districtDAO = ResolverContainer.Resolver.Resolve<IDistrictDAO>();
            _buildingDAO = ResolverContainer.Resolver.Resolve<IBuildingDAO>();
            buildingView.Districts.DataSource = _districtDAO.GetAll(false);
            _buildingView = buildingView;
            buildingView.ShowDialog();
        }



        private void SaveBuildingEvent(object sender, EventArgs eventArgs)
        {
            _building.District = (IDistrict) _buildingView.Districts.Current;
            if (_buildingDAO.SaveOrUpdateAndCommit(_building))
            {
                _buildingView.Close();
            }
        }

    }
}
